package com.yuke.cloud.service.omc.service.impl;

import com.yuke.cloud.common.util.PublicUtil;
import com.yuke.cloud.service.omc.dao.OdsStockOrderMapper;
import com.yuke.cloud.service.omc.entity.OdsOrderGoods;
import com.yuke.cloud.service.omc.entity.OdsStockGoods;
import com.yuke.cloud.service.omc.entity.OdsStockOrder;
import com.yuke.cloud.service.omc.service.OdsStockGoodsService;
import com.yuke.cloud.service.omc.service.OdsStockOrderService;
import com.yuke.cloud.common.core.support.BaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;


/**
 * Created by wg on 2018/11/22.
 */
@Service
@Transactional
@Slf4j
public class OdsStockOrderServiceImpl extends BaseService<OdsStockOrder> implements OdsStockOrderService {
    @Resource
    private OdsStockOrderMapper odsStockOrderMapper;
    @Resource
    private OdsStockGoodsService odsStockGoodsService;

    @Override
    public Long createoderbyshop(List<Map<String, String>> products1, Long aLong, Object totalpay1, Long aLong1, Long warehouseid) {
        OdsStockOrder odsStockOrder = new OdsStockOrder();
        UUID id = UUID.randomUUID();
        String[] idd = id.toString().split("-");
        String uuid20 = idd[0] + idd[1] + idd[2] + idd[3];
        //12位长度,相加一共生成32位的订单号
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYYYMMddHHmm");
        String format = simpleDateFormat.format(new Date());
        long ids = super.generateId();
        odsStockOrder.setStockOrderId(ids);
        odsStockOrder.setStockOrderSn(uuid20 + format);
        odsStockOrder.setWarehouseId(warehouseid);
        odsStockOrder.setShopId(aLong);
        String s1 = totalpay1.toString();
        String substring = s1.substring(0, s1.lastIndexOf("."));
        odsStockOrder.setAmountPayable(Integer.valueOf(substring));
        odsStockOrder.setPayStatus((byte) 0);
        odsStockOrder.setCreateTime(new Date());
        super.save(odsStockOrder);
        for (int i = 0; i < products1.size(); i++) {
            OdsStockGoods odsStockGoods = new OdsStockGoods();
            Map<String, String> map = products1.get(i);
            odsStockGoods.setStockOrderId(ids);
            odsStockGoods.setSpecId(Long.valueOf(map.get("prod_spec")));
            double product_price_total = Double.parseDouble(map.get("product_price_total"));
            double v = product_price_total;// * 100;
            String s2 = v + "";
            String substring1 = s2.substring(0, s2.lastIndexOf("."));
            odsStockGoods.setStockPrice(Integer.valueOf(substring1));
            odsStockGoods.setStockNumber(Integer.valueOf(map.get("number")));
            odsStockGoods.setGoodsStatus((byte) 0);
            String s = map.get("planday");
            // mod by wg 20190407
            if (PublicUtil.isNotEmpty(s)) {
//            if(s!=null){
//                Integer integer = Integer.valueOf(s.trim());
//                Calendar instance = Calendar.getInstance();
//                instance.setTime(new Date());
//                instance.add(Calendar.DATE, integer);
//                Date time = instance.getTime();
//                odsStockGoods.setInboundPlanTime(time);
                try{
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
                    Date time = sdf.parse(s);
                    odsStockGoods.setInboundPlanTime(time);
                }catch (ParseException e) {
                    log.error("解析时间字符串错误", e);
                }


            }
            odsStockGoodsService.save(odsStockGoods);

        }
        return ids;
    }
}
